package com.dw.etl.core.controller;

import com.dw.etl.common.base.BaseException;
import com.dw.etl.common.base.BaseResult;
import com.dw.etl.common.constant.CommonResultConstant;
import com.dw.etl.common.vo.PaginationVO;
import com.dw.etl.core.api.BaseDbTpcompService;
import com.dw.etl.core.bean.model.BaseDbTpcomp;
import com.dw.etl.core.bean.vo.LayMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * baseDbTpcomp控制器
 * Created by hozeData on 2021/05/28.
 */
@RestController
@RequestMapping("/etl/baseDbTpcomp/")
@CrossOrigin(origins = "*", maxAge = 3600)
@Api(tags="数据库字段类型")
public class BaseDbTpcompController {

    @Autowired
    BaseDbTpcompService baseDbTpcompService;

    @ApiOperation(value="分页查询")
    @RequestMapping(value="findByPage", method=RequestMethod.POST)
    public BaseResult findByPage(@RequestBody BaseDbTpcomp baseDbTpcomp, Integer pageNum, Integer pageSize) {
        PaginationVO<BaseDbTpcomp> pageVO = baseDbTpcompService.findPageByModel(baseDbTpcomp, pageNum, pageSize);
        return new BaseResult(CommonResultConstant.SUCCESS, pageVO);
    }

    @ApiOperation(value="查询")
    @RequestMapping(value="find", method=RequestMethod.GET)
    public LayMsg find(Integer page, Integer limit) {
        List<BaseDbTpcomp> pageVO = baseDbTpcompService.findByModel(new BaseDbTpcomp());
        return LayMsg.success().addCount(pageVO.size()).addData(pageVO);
    }

    @ApiOperation(value="保存")
    @ApiImplicitParam(name="baseDbTpcomp", value="实体", paramType="body", required=true, dataType="baseDbTpcomp")
    @RequestMapping(value="save", method=RequestMethod.POST)
    public BaseResult save(@RequestBody BaseDbTpcomp baseDbTpcomp, HttpServletRequest request)  {
        int num ;
        if(baseDbTpcomp.getIdDbTpcomp()==null) {
            num = baseDbTpcompService.insertSelective(baseDbTpcomp);
        } else {
            num =  baseDbTpcompService.updateByIdSelective(baseDbTpcomp);
        }
        return new BaseResult(CommonResultConstant.SUCCESS, num);
    }

    @ApiOperation(value="详情")
    @ApiImplicitParam(name="idDbTpcomp", value="主键", paramType="query", required=true, dataType="Long")
    @RequestMapping(value="getById", method=RequestMethod.GET)
    public BaseResult getById(Long idDbTpcomp, HttpServletRequest request) throws BaseException {
        if(StringUtils.isEmpty(idDbTpcomp)){
            throw new BaseException("请选择要编辑的项");
        }
        BaseDbTpcomp baseDbTpcomp = baseDbTpcompService.getById(idDbTpcomp);
        return new BaseResult(CommonResultConstant.SUCCESS, baseDbTpcomp);
    }

    @ApiOperation(value="根据主键删除")
    @ApiImplicitParam(name="idDbTpcomp", value="主键", paramType="query", required=true, dataType="Long")
    @RequestMapping(value="delete", method=RequestMethod.GET)
    public BaseResult delete(Long idDbTpcomp, HttpServletRequest request) throws BaseException {
        if(StringUtils.isEmpty(idDbTpcomp)){
            throw new BaseException("请选择要删除的项");
        }
        int num = baseDbTpcompService.deleteById(idDbTpcomp);
        return new BaseResult(CommonResultConstant.SUCCESS, num);
    }



}
